<!-- -*- mode: R -*- -->
  <!--
  ## This program is fre esoftware: you can redistribute it and/or modify
  ## it under the terms of the GNU General Public License as published by
  ## the Free Software Foundation, either version 3 of the License, or
  ## any later version.
  ##
  ## This program is distributed in the hope that it will be useful,
  ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  ## GNU General Public License for more details.
  ##
  ## You should have received a copy of the GNU General Public License
  ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
  -->
  
  <%
setContentType("text/html")
#RApacheOutputErrors(TRUE)
options(hverbose=FALSE,verbose=FALSE)
%>
  
<html>
<head>
<title>Strategico: show item</title>
</head>
<body>

  
  <%
strategico.home <- as.character(Sys.getenv("STRATEGICO_HOME"))
source(file.path(strategico.home, "strategico_util.R"))
project.name <- ifelse(is.null(GET$project), "sample", GET$project)
id <- ifelse(is.null(GET$id), 1, as.numeric(GET$id))
param <- ifelse(is.null(GET$param), "", GET$param)
value <- ifelse(is.null(GET$value), "V1", GET$value)
project.config <- Project.GetConfig(project.name=project.name)

keys <- GetKeyNames(project.config$keys)
item.path <- Item.GetUrl(project.name=project.name, id=id, value=value)

db.channel <- DB.Connect()

item.keys <- Item.GetKeys(project.name=project.name, id=id)
item.summary <- Item.DB.GetSummary(project.name=project.name, id=id, db.channel=db.channel, value=value)
item.results <- Item.DB.GetResults(project.name=project.name, id=id, db.channel=db.channel, value=value)

keys.string <- ifelse(is.null(item.keys),
                      "",
                      paste(item.keys, collapse=" - "))

if (is.null(GET$eval) & is.data.frame(item.summary) & nrow(item.summary) == 1)
  param <- as.character(item.summary[1,]$Parameters)
%>
  <center>
    <h1><img src="http://www.r-project.org/Rlogo.jpg" />Strategico :: <%=project.name%> :: <%=keys.string%></h1>
  </center>
  
                <form method="GET">
                <%=BuildHtmlElement_input(name="project", default=project.name, type="hidden")%>
                <%=BuildHtmlElement_input(label="ID", name="id", default=id, type="text", size=4)%>
                <%=BuildFormElement_value(project.name=project.name, default=value)%><br />
                <%=BuildHtmlElement_input(label="Params", name="param", default=param, type="text", size=150)%><br />
                <input type="submit" name="show" value="Show">
                <input type="submit" name="eval" value="Run">
                </form><form action="search-item.brew" method="GET">
                <%=BuildHtmlElement_input(name="project", default=project.name, type="hidden")%>
                <%=BuildHtmlElement_input(name="id", default=id, type="hidden")%>
                <%=BuildHtmlElement_input(name="value", default=value, type="hidden")%><br />
                <input type="submit" name="search_item" value="New search">
                </form>
<% 
if (!is.null(GET$eval)) {
  param <- Param.EvalString(param)
  Item.Eval(project.name=project.name, project.config=project.config, value=value, id=id,
           db.channel=db.channel, param=param)
  }
DB.Close(db.channel)
%>
<h2>Models</h2>
  <img src="<%=paste(item.path,"best_model.png",sep="/")%>" />
  <img src="<%=paste(item.path,"all_models.png",sep="/")%>" />

<h2>Summary</h2>
<%
if(!is.null(item.summary) & !is.na(item.summary) & is.data.frame(item.summary) & nrow(item.summary) > 0) {
  item.summary$Parameters <- NULL
  item.summary <- t(item.summary)
  print(xtable(item.summary), type="html")
}
%>
<h2>Results</h2>
<%
if(!is.null(item.results) & !is.na(item.results) & is.data.frame(item.results) & nrow(item.results) > 0) {
  item.results$id <- NULL
  item.results$item_id <- NULL
  rownames(item.results) <- NULL
  print(xtable(item.results), type="html")
}
%>

<% ShowTemplate('footer') %>

